package org.teremail.schema;

import static java.lang.String.format;

public class H2Dialect extends AbstractDialect {

    private final static H2Dialect instance = new H2Dialect();
    
    private H2Dialect() {}
    
    public static Dialect getInstance() {
        return instance;
    }

    public String getInteger(int precision) {
        switch (precision) {
        case 4:
            return "INT";
        case 8:
            return "BIGINT";
        default:
            throw new RuntimeException("Integer precision not supported: " + precision);
        }
    }

    public String generateBinary(int length) {
        return "BINARY(" + length + ")";
    }

    @Override
    public boolean isDuplicateKey(String state) {
        return "23001".equals(state);
    }

    /**
     * @see org.teremail.schema.AbstractDialect#createPagedQuery(java.lang.String, java.lang.String, java.lang.String, java.lang.String, int, int)
     */
    @Override
    public String createPagedQuery(String columns, String tables,
            String constraints, String orderBy, int pageNo, int pageSize) {
        return format("SELECT %s FROM %s WHERE %s ORDER BY %s LIMIT %d OFFSET %d", 
                      columns, tables, constraints, orderBy, pageSize, (pageNo * pageSize));
    }

    @Override
    public boolean isPagingSupported() {
        return true;
    }
}
